/**
 *  用于自动生成字体映射文件的node文件
 * */
const path = require('path');
const oldPath = path.resolve(__dirname, 'iconfont.svg');
const newPath = path.resolve(__dirname, 'iconfont.json');

var gen = module.exports = function () {
  console.log('========开始生成字体映射文件=======');
  const readline = require('readline');
  const fs = require('fs');
  const fRead = fs.createReadStream(oldPath);
  const fWrite = fs.createWriteStream(newPath, { flags: 'w+', defaultEncoding: 'utf8' });

  const objReadLine = readline.createInterface({
    input: fRead
  });

  var ret = {};
  objReadLine.on('line', line => {
    line = line && line.trim();
    if (line.includes('glyph-name') && line.includes('unicode')) {

      var keyMatch = line.match(/glyph-name="(.*?)"/);
      var key = keyMatch && keyMatch[1];
      key = 'icon-' + key
      var valueMatch = line.match(/unicode="&#(.*?);"/);
      var value = valueMatch && valueMatch[1];
      key && value && (ret[key] = Number(value));
    }
    return;

  });

  objReadLine.on('close', () => {
    console.log('========字体文件映射完成=======');
    fWrite.write(JSON.stringify(ret), 'utf8');
  });
};

gen();